package com.egao.common.module.dispatch.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.utils.CoreUtil;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.dispatch.entity.Dispatch;
import com.egao.common.module.dispatch.mapper.DispatchMapper;
import com.egao.common.module.dispatch.service.DispatchService;
import com.egao.common.module.student.constants.StudentPovertyConstants;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.entity.StudentPoverty;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.entity.Role;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.mapper.CollegeMapper;
import com.egao.common.module.system.service.CollegeService;
import com.egao.common.module.system.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 派遣信息接口实现类
 *
 * @author pyx
 * @Date 2021/6/25
 */
@Service
public class DispatchServiceImpl extends ServiceImpl<DispatchMapper, Dispatch> implements DispatchService {

    @Autowired
    private StudentService studentService;
    @Autowired
    private DictService dictService;


    @Override
    public List<Dispatch> listPage(PageParam<Dispatch> page) {
        List<Dispatch> dispatches = baseMapper.listPage(page);
        return dictType(dispatches);
    }


    @Override
    public JsonResult importExcel(MultipartFile file) {

        StringBuilder sb = new StringBuilder();
        int nums = 0;
        try {
            // 读取excel
            int startRow = 1;
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream(), 0);
            List<List<Object>> list = reader.read(startRow);
            // 进行非空和重复检查
            sb.append(CoreUtil.excelCheckBlank(list, startRow, 0, 1));
            sb.append(CoreUtil.excelCheckRepeat(list, startRow, 1));
            if (!sb.toString().isEmpty()) {
                return JsonResult.error(sb.toString());
            }
            // 进行数据库层面检查
            List<Dispatch> listDispatch = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                List<Object> objects = list.get(i);
                String collegeName = String.valueOf(objects.get(0)); // 学院
                String xh = String.valueOf(objects.get(1));  // 学号
                String studentName = String.valueOf(objects.get(2));  // 姓名
                String byqxdm = String.valueOf(objects.get(3));  // 毕业去向代码
                String bdzqflbdm = String.valueOf(objects.get(4));  // 报到证签发类别代码
                String companyName = String.valueOf(objects.get(5));  // 单位名称
                String companyCode = String.valueOf(objects.get(6));  // 单位代码
                String companySubjection = String.valueOf(objects.get(7));  // 单位隶属
                String companyAddressCode = String.valueOf(objects.get(8));  // 单位所在地代码
                String xzqh = String.valueOf(objects.get(9));  // 单位所在地
                String companyNature = String.valueOf(objects.get(10));  // 单位性质
                String jobCategory = String.valueOf(objects.get(11));  // 就业类别
                String waitCategory = String.valueOf(objects.get(12));  // 未就业类别
                String jobState = String.valueOf(objects.get(13));  // 就业状态
                String k1 = "";
                if(objects.size() > 14){
                    k1 = String.valueOf(objects.get(14));  // 扩展1
                }
                String k5 = "";
                if(objects.size() > 15){
                    k5 = String.valueOf(objects.get(15));  // 扩展5
                }
                String companyOrganization = "";
                if(objects.size() > 16){
                    companyOrganization = String.valueOf(objects.get(16));  // 单位组织机构
                }
                String industry = "";
                if(objects.size() > 17){
                    industry = String.valueOf(objects.get(17));  // 单位行业代码
                }
                String workJobCategory = "";
                if(objects.size() > 18){
                    workJobCategory = String.valueOf(objects.get(18));  // 工作职位
                }
                String hbzdcy = "";
                if(objects.size() > 19){
                    hbzdcy = String.valueOf(objects.get(19));  // 湖北重点产业
                }
                String sjszddm = "";
                if(objects.size() > 20){
                    sjszddm = String.valueOf(objects.get(20)); // 实际所在地代码
                }
                String companyContactName = "";
                if(objects.size() > 21){
                    companyContactName = String.valueOf(objects.get(21));  // 单位联系人
                }
                String companyContactMobile = "";
                if (objects.size() > 22) {
                    companyContactMobile = String.valueOf(objects.get(22));  // 单位联系方式
                }
                String daclqk = "";
                if(objects.size() > 23){
                    daclqk = String.valueOf(objects.get(23));  // 档案转递单位名称
                }
                String dazddz = "";
                if(objects.size() > 24){
                    dazddz = String.valueOf(objects.get(24)); // 档案转递地址
                }
                String dazddh = "";
                if (objects.size() > 25) {
                    dazddh = String.valueOf(objects.get(25));  // 档案转递电话
                }


                Student student = studentService.getByXh(xh);
                if (student == null) {
                    continue;
                }

                baseMapper.delete(new QueryWrapper<Dispatch>()
                        .eq("student_id", student.getId()));

                Dispatch dispatch = new Dispatch();
                dispatch.setStudentId(student.getId());


                dispatch.setByqxdm(byqxdm);
                dispatch.setBdzqflbdm(bdzqflbdm);
                dispatch.setCompanyName(companyName);
                dispatch.setCompanyCode(companyCode);
                dispatch.setCompanySubjection(companySubjection);
                dispatch.setCompanyAddressCode(companyAddressCode);
                dispatch.setXzqh(xzqh);
                dispatch.setCompanyNature(companyNature);
                dispatch.setJobCategory(jobCategory);
                dispatch.setWaitCategory(waitCategory);
                dispatch.setJobState(jobState);
                dispatch.setK1(k1);
                dispatch.setK5(k5);
                dispatch.setCompanyOrganization(companyOrganization);
                dispatch.setIndustry(industry);
                dispatch.setWorkJobCategory(workJobCategory);
                dispatch.setHbzdcy(hbzdcy);
                dispatch.setSjszddm(sjszddm);
                dispatch.setCompanyContactName(companyContactName);
                dispatch.setCompanyContactMobile(companyContactMobile);
                dispatch.setDaclqk(daclqk);
                dispatch.setDazddz(dazddz);
                dispatch.setDazddh(dazddh);


                listDispatch.add(dispatch);
                nums++;
            }
            if (!sb.toString().isEmpty()) {
                return JsonResult.error(sb.toString());
            }
            // 开始添加用户
            int okNum = 0, errorNum = 0;
            for (Dispatch d : listDispatch) {
                if (baseMapper.insert(d) == 1) {
                    okNum++;
                } else {
                    errorNum++;
                }
            }
            return JsonResult.ok("导入完成，成功" + okNum + "条，失败" + errorNum + "条");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(nums + "条数据出现问题");
        }
        return JsonResult.error("导入失败");
    }

    private List<Dispatch> dictType(List<Dispatch> listDispatch){
        List<Dispatch> list = new ArrayList<>();
        for (Dispatch dispatch:listDispatch) {
            dispatch.setByqx(dictService.cacheLabel("sign_byqx",dispatch.getByqxdm()));
            dispatch.setBdzqflb(dictService.cacheLabel("sign_bdzqflb",dispatch.getBdzqflbdm()));
            dispatch.setCompanySubjectionName(dictService.cacheLabel("sign_dwls",dispatch.getCompanySubjection()));
            list.add(dispatch);
        }
        return list;
    }
}
